package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.SysMenu;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-04-24
 */
@Api(tags = "权限表")
@RestController
@RequestMapping("/sys-menu")
public class SysMenuController {


    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    public Result getById(String id) {
        return Result.successResult(sysMenuService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody SysMenu sysMenu) {
        sysMenuService.save(sysMenu);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String ids) {
        String[] idsStrs = ids.split(",");
        for (String id : idsStrs) {
            sysMenuService.removeById(Long.parseLong(id));
        }
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return Result.successResult(null);
    }


    /**
     * 查询所有权限点
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有权限点", notes = "查询所有权限点", httpMethod = "GET")
 /*   @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })*/
    public Result list() {
         LambdaQueryWrapper<SysMenu> lambda = new QueryWrapper<SysMenu>().lambda();
        lambda.eq(SysMenu::getDelFlag,0);
        lambda.eq(SysMenu::getCategory,1);
        List<SysMenu> list = sysMenuService.list(lambda);
        for (int i =0 ;i < list.size() ;i++){
            LambdaQueryWrapper<SysMenu> lambda1 = new QueryWrapper<SysMenu>().lambda();
            lambda1.eq(SysMenu::getDelFlag,0);
            lambda1.eq(SysMenu::getCategory,2);
            lambda1.eq(SysMenu::getParentId,list.get(i).getId());
            list.get(i).setSysMenuList( sysMenuService.list(lambda1));
        }
        return Result.successResult(list);
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<SysMenu> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<SysMenu> lambda = new QueryWrapper<SysMenu>().lambda();
        //TODO 自己的查询逻辑
        return Result.successResult(this.sysMenuService.page(page, lambda));
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<SysMenu> lambda, SysMenuReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(SysMenu::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getParentId())) {
            lambda.eq(SysMenu::getParentId, param.getParentId());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(SysMenu::getName, param.getName());
        }
        if (!StringUtils.isEmpty(param.getCode())) {
            lambda.eq(SysMenu::getCode, param.getCode());
        }
        if (!StringUtils.isEmpty(param.getPath())) {
            lambda.eq(SysMenu::getPath, param.getPath());
        }
        if (!StringUtils.isEmpty(param.getCategory())) {
            lambda.eq(SysMenu::getCategory, param.getCategory());
        }
        if (!StringUtils.isEmpty(param.getPerms())) {
            lambda.eq(SysMenu::getPerms, param.getPerms());
        }
        if (!StringUtils.isEmpty(param.getLayout())) {
            lambda.eq(SysMenu::getLayout, param.getLayout());
        }
        if (!StringUtils.isEmpty(param.getIcon())) {
            lambda.eq(SysMenu::getIcon, param.getIcon());
        }
        if (!StringUtils.isEmpty(param.getVisible())) {
            lambda.eq(SysMenu::getVisible, param.getVisible());
        }
        if (!StringUtils.isEmpty(param.getSort())) {
            lambda.eq(SysMenu::getSort, param.getSort());
        }
        if (!StringUtils.isEmpty(param.getStatus())) {
            lambda.eq(SysMenu::getStatus, param.getStatus());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(SysMenu::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(SysMenu::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(SysMenu::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(SysMenu::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(SysMenu::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(SysMenu::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(SysMenu::getDelFlag, param.getDelFlag());
        }
        lambda.orderBy(true, false, SysMenu::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class SysMenuReqVo extends SysMenu {
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
